package com.wu.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {
    private static final String TOKEN = "dfhau!@#$%d215*&()ds11";

    /**
     * 生成TOKEN
     *
     * @param map  //传入payload
     * @return     // 返回token
     */
    public static String getToken(Map<String,String> map){
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k,v) ->{
            builder.withClaim(k,v);
        });
        //设置过期时间
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND,100);
        builder.withExpiresAt(instance.getTime());
        //加密
        return builder.sign(Algorithm.HMAC256(TOKEN)).toString();
    }


    /**
     * 验证token
     * @param token
     */
    public static DecodedJWT verify(String token){
        return  JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
    }

//    /**
//     * 获取token的payload,,,,,,,,,,,,,,,,,将上面返回改为DecodedJWT这个方法就可以不写
//     * @param token
//     * @return
//     */
//    public static DecodedJWT getToken(String token){
//        return JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
//    }






























}
